<?php

namespace App\Exports;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class PointStudentFailExport  extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{
    private $guardUserId;
    public function __construct($id) {
        $this->guardUserId = $id;
    }

    public function array():array  {
        $data = [
            [
                '姓名',
                '电话',
                '邮箱',
                '志愿学校',
                '志愿专业',
                '奖学金',
                '预计总学费',
                '调剂学校',
                '调剂专业',
                '奖学金',
                'GPA',
                '复试总成绩',
                '预计总学费',
                '面试成绩',
                '来源',
                '失败原因'
            ]
        ];
        
        Log::info('PointStudentFailExport');
        $records = Cache::get('point_'.$this->guardUserId);
        Log::info('PointStudentFailExport',compact('records'));
        foreach ($records as &$record) {
            $data[] = $record;
        }
        return $data;
    }

    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
